﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class ExamGroup : System.Web.UI.Page
    {
        static int schoolid = Config.CurrentSchoolID;
        static int examgroupid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myExamGroup = from c in db.ExamGroup
                                  select c;
                    this.Store2.DataSource = myExamGroup;
                    this.Store2.DataBind();
                }
            }
        }

        //protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        schoolid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myShool = from c in db.SchoolInfo
        //                      where c.SchoolID == schoolid
        //                      select c.FullName;
        //        foreach (var school in myShool)
        //        {
        //            DropDownField1.Text = school.ToString();
        //        }
        //        var myExamGroup = from c in db.ExamGroup
        //                      where c.SchoolID == schoolid
        //                      select c;
        //        this.Store2.DataSource = myExamGroup;
        //        this.Store2.DataBind();
        //    }
        //    schoolid = 0;
        //}

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            GroupName.Text = "";
            GroupType.Text = "";
            //schoolid = 0;
            this.FormWindow.Visible = true;
            this.FormWindow.Show();
            Button6.Show();
            Button7.Hide();
        }
        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examgroupid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newegroup = from c in db.ExamGroup
                                    where c.ExamGroupID == examgroupid
                                    select c;
                    ExamAnalysis.Model.ExamGroup newe = newegroup.First();
                    //var school = from s in db.SchoolInfo
                    //             where s.SchoolID == newe.SchoolID
                    //             select s.FullName;
                    //schoolid = newe.SchoolID;
                    GroupName.Text = newe.GroupName;
                    GroupType.Text = newe.GroupType;
                    //DropDownField2.Text = school.First().ToString();
                }
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Hide();
                Button7.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个分组").Show();
            }
        }
        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    examgroupid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newegroup = from c in db.ExamGroup
                                    where c.ExamGroupID == examgroupid
                                    select c;
                    ExamAnalysis.Model.ExamGroup neweg = newegroup.First();
                    db.ExamGroup.DeleteOnSubmit(neweg);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("ExamGroup.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个分组").Show();
            }
        }

        //protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        //{
        //    RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        //    foreach (SelectedRow row in sm.SelectedRows)
        //    {
        //        schoolid = Convert.ToInt32(row.RecordID);
        //    }
        //    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
        //    {
        //        var myShool = from c in db.SchoolInfo
        //                      where c.SchoolID == schoolid
        //                      select c.FullName;
        //        foreach (var school in myShool)
        //        {
        //            DropDownField2.Text = school.ToString();
        //        }
        //    }
        //}

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            //if (DropDownField2.Text.ToString().Equals("请选择"))
            //{
            //    X.Msg.Alert("提示信息", String.Format("请选择一个学校")).Show();
            //}
            //else
            //{
                ExamAnalysis.Model.ExamGroup neweg = new ExamAnalysis.Model.ExamGroup();
                neweg.GroupName = GroupName.Text;
                neweg.GroupType = GroupType.Text;
                neweg.SchoolID = schoolid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    db.ExamGroup.InsertOnSubmit(neweg);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功添加分组")).Show();
                   // schoolid = 0;
                }
                Response.Redirect("ExamGroup.aspx");
           // }
        }

        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newegroup = from c in db.ExamGroup
                                where c.ExamGroupID == examgroupid
                                select c;
                ExamAnalysis.Model.ExamGroup neweg = newegroup.First();
                neweg.GroupName = GroupName.Text;
                neweg.GroupType = GroupType.Text;
                neweg.SchoolID = schoolid;
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改分组信息")).Show();
                //schoolid = 0;
                examgroupid = 0;
            }
            Response.Redirect("ExamGroup.aspx");
        }
    }
}